package com.ford.trade.controller;

import com.ford.trade.dto.QueryDTO;
import com.ford.trade.entity.AoKeGame;
import com.ford.trade.processor.AoKeOdds3Processor;
import com.ford.trade.processor.AoKeTeamProcessor;
import com.ford.trade.processor.TeamProcessor;
import com.ford.trade.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: ford
 * @date: 2022/4/25 11:33
 */
@Slf4j
@RestController
public class AokeController {

    @Resource
    private Game500Service game500Service;
    @Resource
    private PeilvService peilvService;
    @Resource
    private FenxiService fenxiService;
    @Resource
    private AoKeGameService aoKeGameService;
    @Resource
    private OddsService oddsService;
    @Resource
    private AokeFenxiService aokeFenxiService;
    @Resource
    private Odds3Service odds3Service;


    @PostMapping("/aoke/getBisai")
    public void getBisai(@RequestBody QueryDTO queryDTO) {
        if ("500".equals(queryDTO.getUrlType())) {
            TeamProcessor teamProcessor = new TeamProcessor();
            teamProcessor.setGameService(game500Service);
            teamProcessor.setDate(queryDTO.getDate());
            teamProcessor.setUrl(queryDTO.getUrl());
            teamProcessor.setGetResult(queryDTO.getGetResult());
            Spider.create(teamProcessor)
                    .addUrl(queryDTO.getUrl())
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100)))
                    .thread(10)
                    .run();
        } else {
            AoKeTeamProcessor teamProcessor = new AoKeTeamProcessor();
            teamProcessor.setGameService(aoKeGameService);
            teamProcessor.setWeek(queryDTO.getDate());
            teamProcessor.setUrl(queryDTO.getUrl());
            teamProcessor.setGetResult(queryDTO.getGetResult());
            Spider.create(teamProcessor)
                    .addUrl(queryDTO.getUrl())
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100)))
                    .thread(10)
                    .run();
        }
    }

    @PostMapping("/aoke/getOdds")
    public String getOdds3(@RequestBody QueryDTO queryDTO) throws IOException, ParseException {
        List<AoKeGame> allByDateAfter = aoKeGameService.findAllByDateBetten(queryDTO.getDate());
        if (CollectionUtils.isEmpty(allByDateAfter)) {
            return "未查询到比赛";
        }
        List<String> urls = new ArrayList<>();
        List<AoKeGame> collect = allByDateAfter.stream().skip(queryDTO.getSkip()).limit(queryDTO.getLimit()).collect(Collectors.toList());
        int i = 1000;
        for (AoKeGame aoKeGame : collect) {
            i += 1000;
            AoKeOdds3Processor aoKeOddsProcessor = new AoKeOdds3Processor();
            aoKeOddsProcessor.setOddsService(odds3Service);
            aoKeOddsProcessor.setDate(queryDTO.getDate());
            aoKeOddsProcessor.setCookie("LastUrl=; FirstURL=www.okooo.com/; FirstOKURL=https%3A//jingcai.okooo.com/; First_Source=www.okooo.com; M_UserName=%22%5Cu98de%5Cu9e1f%5Cu96f7%5Cu9e23%22; M_UserID=31747680; M_Ukey=39755d37eb1ef335ded3a5b5f523199c; data_start_isShow=1; __utmz=56961525." + System.currentTimeMillis() + i + ".13.5.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; zq_companytype_odds=MyOddsBooks2; __utmc=56961525; Hm_lvt_213d524a1d07274f17dfa17b79db318f=" + (System.currentTimeMillis() + i - 588108 - 511906 - 442681) + "," + (System.currentTimeMillis() + i - 588108 - 511906 - 442681 - 588108) + "," + (System.currentTimeMillis() + i - 588108 - 511906) + "," + (System.currentTimeMillis() + i - 588108) + "; pm=; PHPSESSID=2839acf266ece5c71f4b6eca3d32655b9b94d38b; IMUserID=31747680; IMUserName=%E9%A3%9E%E9%B8%9F%E9%9B%B7%E9%B8%A3; OKSID=2839acf266ece5c71f4b6eca3d32655b9b94d38b; OkAutoUuid=3c32b71b3dd64bb12d46361e5a141417; OkMsIndex=3; __utma=56961525.1953077866.1659002210.1662359132.1662361803.24; acw_tc=2f61f27416623637233844951e57db43c5f146e29bea9316ff168d1faccbf3; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=" + System.currentTimeMillis() + i / 1000 + "; __utmb=56961525.55.9." + System.currentTimeMillis() + i);
            urls.add("https://www.okooo.com/soccer/match/" + aoKeGame.getIndexId() + "/odds/ajax/?page=0&trnum=0&companytype=MyOddsBooks2&type=1");
            aoKeOddsProcessor.setIndexId(aoKeGame.getIndexId().toString());
            Spider.create(aoKeOddsProcessor)
                    .addUrl("https://www.okooo.com/soccer/match/" + aoKeGame.getIndexId() + "/odds/ajax/?page=0&trnum=0&companytype=MyOddsBooks2&type=1")
                    .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(1000)))
                    .thread(1)
                    .run();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "比赛总数:" + allByDateAfter.size();
    }

}
